package cn.zero.security.core.mobile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

@Component
public class MobileAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
	
	@Autowired
	private AuthenticationSuccessHandler appAuthenticationSuccessHandler;
	
	@Autowired
	private AuthenticationFailureHandler appAuthenticationFailureHandler;
	
	@Autowired
	private UserDetailsService userDetailsService;
	
//	@Autowired
//	private PersistentTokenRepository persistentTokenRepository;
	
	/* (non-Javadoc)
	 * @see org.springframework.security.config.annotation.SecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.SecurityBuilder)
	 */
	@Override
	public void configure(HttpSecurity http) throws Exception {
		
		MobileAuthenticationFilter MobileAuthenticationFilter = new MobileAuthenticationFilter();
		MobileAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
		MobileAuthenticationFilter.setAuthenticationSuccessHandler(appAuthenticationSuccessHandler);
		MobileAuthenticationFilter.setAuthenticationFailureHandler(appAuthenticationFailureHandler);
		//String key = UUID.randomUUID().toString();
		//MobileAuthenticationFilter.setRememberMeServices(new PersistentTokenBasedRememberMeServices(key, userDetailsService, persistentTokenRepository));
		
		MobileAuthenticationProvider MobileAuthenticationProvider = new MobileAuthenticationProvider();
		MobileAuthenticationProvider.setUserDetailsService(userDetailsService);
		
		http.authenticationProvider(MobileAuthenticationProvider)
			.addFilterAfter(MobileAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
		
//		
		
	}

}